package org.emekteb.auth;

import javax.validation.Valid;

import org.emekteb.entity.Login;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/welcome")
public class LoginController {

	@RequestMapping(method=RequestMethod.GET)
	public String getLoginForm(Model model) {
		model.addAttribute(new Login());
		return "/welcome";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String authenticate(@Valid Login login, BindingResult result) {
		if (result.hasErrors() ||
			!(login.getUserId().equals("admin") &&
			  login.getPassword().equals("test"))) {
			return "/welcome";
		}
		return "redirect:/account/test/56/" + login.getId();
	}
}
